﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AVCare.LMS.Models;
using AVCare.LMS.DataAccess;
using AVCare.LMS.Util;
using System.Web.Security;

namespace AVCare.LMS.Web.Controllers
{
    public class MenuController : Controller
    {
        //
        // GET: /Menu/
        public ActionResult MenuList()
        {
            List<Function> menus = Session["menus"] as List<Function>;
            if (Request.IsAuthenticated)
            {
                List<Role> roles = HelperProvider.GetUserHelper().GetUserRole(User.Identity.Name);
                string[] rolesIDs = roles.Select(r => r.ID).ToArray();
                menus = HelperProvider.GetMenuHelper().GetMenus(rolesIDs);
                Session.Add("menus", menus);
            }
            else
            {
                menus = new List<Function>();
            }
            return View("Menus", menus);
        }

        public JsonResult SubMenu(string id)
        {
            Dictionary<string, List<Function>> subMenus = Session["submenus"] as Dictionary<string, List<Function>>;
            if (subMenus == null)
            {
                subMenus = new Dictionary<string, List<Function>>();
                Session.Add("submenus", subMenus);
            }

            if (subMenus.Keys.Contains(id))
            {
                return Json(subMenus[id]);
            }
            List<Role> roles = HelperProvider.GetUserHelper().GetUserRole(User.Identity.Name);
            string[] rolesIDs = roles.Select(r => r.ID).ToArray();

            List<Function> menus = HelperProvider.GetMenuHelper().GetSubMenus(id, rolesIDs);

            foreach (var item in menus)
            {
                item.Name = LanguageHelper.GetString(item.Name);
            }
            subMenus.Add(id, menus);
            return Json(menus);
        }
    }
}
